package com.das.mechanic_base.adapter.maintain;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.maintain.CancelWorkBean;

import java.util.ArrayList;
import java.util.List;

public class X3TainCancelAdapter extends RecyclerView.Adapter<X3TainCancelAdapter.ServiceHolder>{

    private Context mContext;

    private List<CancelWorkBean> cancelWorkBeans;

    private List<CancelWorkBean> mList;

    private List<Boolean> bList;

    public X3TainCancelAdapter(Context mContext, List<CancelWorkBean> serviceBaseListBeans) {
        this.mContext = mContext;
        this.cancelWorkBeans = serviceBaseListBeans;
        bList = new ArrayList<>();
        mList = new ArrayList<>();
        for (int i = 0; i < cancelWorkBeans.size(); i++) {
            bList.add(i,false);
        }
    }

    @NonNull
    @Override
    public ServiceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.x3_cancel_service_item,parent,false);
        ServiceHolder service_time_item = new ServiceHolder(view);
        return service_time_item;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(@NonNull ServiceHolder holder, final int position) {

        holder.tv_service.setText(cancelWorkBeans.get(position).getServiceBaseName() + "");

        if (bList.get(position)) {
            holder.itemView.setBackground(mContext.getDrawable(R.drawable.x3_store_select));
            holder.tv_service.setTextColor(Color.parseColor("#0077FF"));

        }else {
            holder.itemView.setBackground(mContext.getDrawable(R.drawable.x3_store_no_select));
            holder.tv_service.setTextColor(Color.parseColor("#333333"));
        }


        holder.itemView.setOnClickListener(v -> {

            mList.clear();
            for (int i = 0; i < bList.size(); i ++) {
                if (i == position) {
                    if (bList.get(i)) {
                        bList.remove(i);
                        bList.add(i,false);
                    }else {
                        bList.remove(i);
                        bList.add(i,true);
                    }
                }

                if (bList.get(i)) {
                    mList.add(cancelWorkBeans.get(i));
                }
            }

            notifyDataSetChanged();

            if (iOnClick != null) {
                iOnClick.iCancelOnClickListener(mList,mList.size() == cancelWorkBeans.size() ? true : false);
            }
        });
    }

    @Override
    public int getItemCount() {
        return cancelWorkBeans.size();
    }

    IOnClick iOnClick;

    public void setiOnClick(IOnClick iOnClick) {
        this.iOnClick = iOnClick;
    }

    public interface IOnClick {
        void iCancelOnClickListener(List<CancelWorkBean> mList,boolean isOne);
    }

    class ServiceHolder extends RecyclerView.ViewHolder{

        TextView tv_service;
        public ServiceHolder(View itemView) {
            super(itemView);
            tv_service = itemView.findViewById(R.id.tv_service);
        }
    }
}
